// pages/cart/cart.js
import {getSetting,chooseAddress,openSetting,showModal,showToast} from "../../utils/asyncWx.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:{},
    cart:[],
    allChecked:false,
    totalPrice:0,
    totalNum:0
  },

  // 选择地址
  async handleChooseAddress(){
    try {
      const res1= await getSetting();
      const scopeAddress= res1.authSetting["scope.address"];
      if(scopeAddress===false){    
        await openSetting();
      }
      let address=await chooseAddress();
      address.all=address.provinceName+address.cityName+address.countyName+address.detailInfo;
      wx.setStorageSync('address', address);//写入缓存
    } catch(error){
      console.log(error);
    }
   
  },

  // 当页面发生更改时，总价总量全选状态需要重新计算
  setCart(cart){
    let allChecked=true;
    let totalPrice=0;
    let totalNum=0;
    cart.forEach(v=>{
      if(v.checked){
        totalPrice+=v.num*v.goods_price;
        totalNum+=v.num;
      }
      else{
        allChecked=false;
      }
    })
    if(cart.length===0)
      allChecked=false;
    this.setData({
      cart,allChecked,totalNum,totalPrice
    });
    wx.setStorageSync('cart', cart);
  },

  // 每个商品的选择框
  handleItemChange(e){
    const goods_id=e.currentTarget.dataset.id;
    // 获取购物车数组
    let {cart}=this.data;
    // 获取该商品在购物车中的索引
    let index=cart.findIndex(v=>v.goods_id==goods_id);
    // 索引取反
    cart[index].checked=!cart[index].checked;
    this.setCart(cart);
  },

  // 全选按钮
  handleItemAllCheck(){
    let {cart,allChecked}=this.data;
    allChecked=!allChecked;
    cart.forEach(v=>v.checked=allChecked);
    this.setCart(cart);
  },

  // 数量控制
  async handleItemNumEdit(e){
    const {id,operation}=e.currentTarget.dataset;
    let {cart}=this.data;
    let index=cart.findIndex(v=>v.goods_id===id);
    if(cart[index].num===1&&operation===-1){
      const res=await showModal({content:"您是否要删除？",});
      if(res.confirm){
        cart.splice(index,1);
        this.setCart(cart);
      }
    }
    else{
      cart[index].num+=operation;
      this.setCart(cart);
    }
    
  },

  async handlePay(e){
    const {address,totalNum}=this.data;
    if(!address.userName){
      await showToast({title:"您还没有选择收货地址"});
      return;
    }
    if(totalNum===0){
      await showToast({title:"您还没有选择商品"});
      return;
    }
    wx.navigateTo({
      url: '/pages/pay/pay'
    })
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address=wx.getStorageSync('address');
    const cart=wx.getStorageSync('cart')||[];
    //const allChecked=cart.length?cart.every(v=>v.checked):false;//必须每个遍历item都为true才能返回true,空数组返回值是true
    this.setData({address});
    this.setCart(cart);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})